package com.xxh.dataaccess.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxh.dataaccess.dao.ChatMessageDao;
import com.xxh.dataaccess.dao.UserDao;
import com.xxh.dataaccess.dto.ContactMessage;
import com.xxh.dataaccess.dto.ContactMessageDto;
import com.xxh.dataaccess.dto.Contacts;
import com.xxh.dataaccess.dto.FromUser;
import com.xxh.dataaccess.entity.ChatMessage;
import com.xxh.dataaccess.entity.User;
import com.xxh.dataaccess.service.ChatMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ChatMessage)表服务实现类
 *
 * @author makejava
 * @since 2021-02-24 20:53:27
 */
@Service("chatMessageService")
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageDao, ChatMessage> implements ChatMessageService {

    @Resource
    ChatMessageDao chatMessageDao;

    @Resource
    UserDao userDao;


    /**
     * 获取用户联系人信息
     *
     * @param uid 用户ID
     * @return
     */
    @Override
    public List<Contacts> getContacts(Integer uid) {
        return chatMessageDao.getContactsByUid(uid);
    }


    @Override
    public List<ContactMessageDto> pullContactMessage(Integer uid , Integer cid) {
        List<ContactMessage> list = chatMessageDao.pullMessage(uid, cid);
        List<ContactMessageDto> dtoList = list.stream().map(item -> {
            ContactMessageDto dto = new ContactMessageDto();
            BeanUtils.copyProperties(item, dto);
            dto.setSendTime(item.getSendTime().getTime());
            User user = userDao.selectById(item.getFromUser());
            FromUser fromUser = new FromUser();
            fromUser.setId(user.getUid());
            fromUser.setAvatar(user.getHeadimgurl());
            fromUser.setDisplayName(user.getUsername());
            dto.setFromUser(fromUser);
            return dto;
        }).collect(Collectors.toList());
        return dtoList;
    }
}
